package org.adorsys.aderp.aderplogin.client.hessian;

import org.adorsys.aderp.aderplogin.client.UserDetailsHelper;
import org.adorsys.aderp.aderplogin.client.common.Oauth2User;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.AuthenticationUserDetailsService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken;

/**
 * This bean is in charge of populating the user detail object with 
 * credentials required to run this app. 
 * 
 * In particular the user name that is required by the application for 
 * login.
 * 
 * It will contact the login application that maps oauth token to
 * a user permission profile and return that permission profile in form of a user detail.
 * 
 * @author francis
 *
 * @param <T>
 */
public class HessianUserDetailsServiceWrapper implements
		AuthenticationUserDetailsService<PreAuthenticatedAuthenticationToken>{

	@Override
	public UserDetails loadUserDetails(PreAuthenticatedAuthenticationToken token)
			throws UsernameNotFoundException {
		Oauth2User principal = (Oauth2User) token.getPrincipal();
		Authentication clientAuthentication = (Authentication) token.getCredentials();
		return UserDetailsHelper.createUserDetails(
				principal,clientAuthentication);
	}

}
